var categoryParams = [];//保存分类的入参
// 保存一、二、三级分类编辑函数
 function saveCategoryFn(els,level) {
     var isSaveEdit = true;
     for (var i=0;i<els.length;i++){
         for(var j=i+1;j<els.length;j++){
             if($(els[i]).find(".category-sort")[0].children[0].value==$(els[j]).find(".category-sort")[0].children[0].value){
                 layer.msg("存在相同的排序，请修改后保存", {icon: 5, time: 3000});
                 isSaveEdit = false;
                 return;
             }
         }
     }
     var frontCategoryList = [];
     if(isSaveEdit){
         for (var i=0;i<els.length;i++){
             var showSequence = 20;
             if($(els[i]).find(".category-show")[0].children[0].checked){
                 showSequence = 10;
             }
             /*var showName = "";
             console.log($(els[i]).find(".show-category-name").html())
             if($(els[i]).find(".show-category-name").html()!=""){
                 showName = $(els[i]).find(".show-category-name").html();
             }else {
                 showName = $(els[i]).find(".category-name").html();
             }*/
             frontCategoryList.push({
                 afterCategoryId:$(els[i]).find(".category-id").html(),
                 showSequence: Number($(els[i]).find(".category-sort")[0].children[0].value),
                 fristCategoryName: $(els[i]).find(".show-category-name").html(),
                 status: showSequence,
             });
         }
         categoryParams = {
             frontCategoryList:frontCategoryList,
             categoryLevel: level,
             source: $("#port_type").val(),
             platform: 10,
             warehouse: 1,
         }
         $.ajax({
             contentType: 'application/json',
             url: root.category.category_updateSecondCategory,
             cache: false,  //禁用缓存
             type: "POST",
             data: JSON.stringify(categoryParams),
             success: function (data) {
                 if(data.errorCode==0){
                     layer.msg("修改成功", {icon: 6, time: 3000});
                     if(level==1){
                         cancel_cate();
                     }else if(level==2){
                         cancel_secondcate(els.parents(".level1").find(".cancel-second-btn")[0]);
                     }else if(level==3){
                         cancel_thirdcate(els.parents(".level2").find(".cancel-third-btn")[0]);
                     }
                 }else {
                     layer.msg(data.errorMessage, {icon: 5, time: 3000});
                 }
             }
         });
     }
 }
 //局部更新分类列表
 function uadateCatagoryList(e) {
     console.log(e)
     console.log($(e).parents(".level1").find(".level2-box"))
     console.log($(e).parents(".category-contents").find(".category-level").html())
     var categoryLevel = $(e).parents(".category-contents").find(".category-level").html();//正在编辑的分类级别
     var afterCategoryId = $(e).parents(".category-contents").find(".category-id").html();//正在编辑的分类id
     $.ajax({
         contentType: 'application/json',
         url: root.category.category_linkedCategory,
         cache: false,  //禁用缓存
         type: "POST",
         data: JSON.stringify({
             afterCategoryIds:[afterCategoryId],
             categoryLevel:categoryLevel,
             source: $("#port_type").val(),
             platform: 10,
             warehouse: 1,
         }),
         success: function (data) {
             if(data.errorCode==0){
                 console.log(data);
                 if(categoryLevel==1){
                     var level2Html = '';
                     var level2List = data.data[0].secondList;
                     if(level2List){
                         for(var j=0;j<level2List.length;j++){
                             var level3Html = '';
                             var level3List = level2List[j].secondList;
                             if(level3List){
                                 for(var k=0;k<level3List.length;k++){
                                     var checkedStr3 = "";
                                     if(level3List[k].status==10){
                                         checkedStr3 = "checked";
                                     }else {
                                         checkedStr3 = "";
                                     }
                                     level3Html += '<div class="level3-content">' +
                                         '                                        <ul class="clearfix" style="width: 100%">' +
                                         '                                            <li class="f-l text-c" style="width:11.5%"></li>' +
                                         '                                            <li class="f-l text-c" style="width:14%"></li>' +
                                         '                                            <li class="f-l text-c" style="width:10%"></li>' +
                                         '                                            <li class="f-l text-c" style="width:13%"></li>' +
                                         '                                            <li class="f-l text-c" style="width:10%"><span class="ml-5 category-name">'+level3List[k].afterCategoryName+'</span><span class="ml-5 category-id hide">'+level3List[k].afterCategoryId+'</span><span class="ml-5 category-level hide">'+level3List[k].categoryLevel+'</span></li>' +
                                         '                                            <li class="f-l text-c" style="width:13%">' +
                                         '                                                <span class="show-category-name">'+dealNull(level3List[k].fristCategoryName)+'</span>' +
                                         '                                                <i class="Hui-iconfont f-16 c-primary change_third do-edit" style="display: none" onclick="changeFirst(this)">&#xe6df;</i>' +
                                         '                                               <b class="remove-showName" style="display: none"><i class="Hui-iconfont c-primary f-16 va-m"  onclick="removeFirst(this)">&#xe6a6;</i></b>' +
                                         '                                                <input type="text" class="input-text w-80" maxlength="15" style="display: none" value="'+dealNull(level3List[k].fristCategoryName)+'">' +
                                         '                                                <span class="edit-btn-box" style="display: none"><i class="Hui-iconfont ml-10 iconsave c-primary f-16 va-m" onclick="saveFirst(this)">&#xe6a7;</i><i class="Hui-iconfont c-primary f-16 va-m iconcancel" onclick="cancelFirst(this)">&#xe6a6;</i></span>' +
                                         '                                            </li>' +
                                         '                                            <li class="f-l text-c category-show" style="width:8%"><input type="checkbox" class="checkbox level3-checkBox" '+checkedStr3+' disabled></li>' +
                                         '                                            <li class="f-l text-c category-sort" style="width:6%"><input type="text" class="input-text w-50 level3Sort text-c" oninput = "value=value.replace(/[^0-9]/g,\'\')" onblur="checksort(this,this.value,100)" value="'+level3List[k].showSequence+'" readonly></li>' +
                                         '                                            <li class="f-l text-c" style="width:14.5%"></li>' +
                                         '                                        </ul>' +
                                         '                                    </div>';
                                 }
                             }
                             var checkedStr2 = "";
                             if(level2List[j].status==10){
                                 checkedStr2 = "checked";
                             }else {
                                 checkedStr2 = "";
                             }
                             level2Html += '<div class="level2">' +
                                 '                                <div class="level2-content category-contents">' +
                                 '                                    <ul class="clearfix" style="width: 100%">' +
                                 '                                        <li class="f-l text-c" style="width:11.5%"></li>' +
                                 '                                        <li class="f-l text-c" style="width:14%"></li>' +
                                 '                                        <li class="f-l text-l" style="width:10%">' +
                                 '                                            <button type="button" class="va-m text-c index_second ml-10" style="width:20px;height:20px" onclick="level3_show(this)">+</button>' +
                                 '                                            <span class="ml-5 category-name">'+level2List[j].afterCategoryName+'</span>' +
                                 '                                            <span class="ml-5 category-id hide">'+level2List[j].afterCategoryId+'</span>' +
                                 '                                            <span class="ml-5 category-level hide">'+level2List[j].categoryLevel+'</span>' +
                                 '                                        </li>' +
                                 '                                        <li class="f-l text-c" style="width:13%">' +
                                 '                                            <span class="show-category-name">'+dealNull(level2List[j].fristCategoryName)+'</span>' +
                                 '                                            <i class="Hui-iconfont f-16 c-primary change_second do-edit" style="display: none" onclick="changeFirst(this)">&#xe6df;</i><b class="remove-showName" style="display: none"><i class="Hui-iconfont c-primary f-16 va-m" onclick="removeFirst(this)">&#xe6a6;</i></b>' +
                                 '                                            <input type="text" class="input-text w-80" maxlength="15" style="display: none" value="'+dealNull(level2List[j].fristCategoryName)+'">' +
                                 '                                            <span class="edit-btn-box" style="display: none"><i class="Hui-iconfont ml-10 iconsave c-primary f-16 va-m" onclick="saveFirst(this)">&#xe6a7;</i><i class="Hui-iconfont iconcancel c-primary f-16 va-m" onclick="cancelFirst(this)">&#xe6a6;</i></span>' +
                                 '                                        </li>' +
                                 '                                        <li class="f-l text-c" style="width:10%"></li>' +
                                 '                                        <li class="f-l text-c" style="width:13%"></li>' +
                                 '                                        <li class="f-l text-c category-show" style="width:8%"><input type="checkbox" class="checkbox level2-checkBox" '+checkedStr2+' disabled></li>' +
                                 '                                        <li class="f-l text-c category-sort" style="width:6%"><input type="text" class="input-text w-50 level2Sort text-c" oninput = "value=value.replace(/[^0-9]/g,\'\')" onblur="checksort(this,this.value,100)" value="'+level2List[j].showSequence+'" readonly></li>' +
                                 '                                        <li class="f-l text-c" style="width:14.5%">' +
                                 '                                            <input type="button" class="btn btn-secondary size-S radius edit-third-btn powerButton 494" value="编辑三级品类" onclick="edit_thirdcate(this)">' +
                                 '                                            <input type="button" class="btn btn-secondary size-S radius save-third-btn" value="保存" style="display:none" onclick="save_thirdcate(this)">' +
                                 '                                            <input type="button" class="btn size-S radius cancel-third-btn" value="取消" style="display:none" onclick="cancel_thirdcate(this)">' +
                                 '                                        </li>' +
                                 '                                    </ul>' +
                                 '                                </div>' +
                                 '                                <div class="level3-box" style="display: none">' +level3Html+
                                 '                                </div>' +
                                 '                            </div>';
                         }
                     }
                     $(e).parents(".level1").find(".level2-box").html(level2Html);
                 }else if(categoryLevel==2){
                     var level3Html = '';
                     var level3List = data.data[0].secondList;
                     if(level3List){
                         for(var k=0;k<level3List.length;k++){
                             var checkedStr3 = "";
                             if(level3List[k].status==10){
                                 checkedStr3 = "checked";
                             }else {
                                 checkedStr3 = "";
                             }
                             level3Html += '<div class="level3-content">' +
                                 '                                        <ul class="clearfix" style="width: 100%">' +
                                 '                                            <li class="f-l text-c" style="width:11.5%"></li>' +
                                 '                                            <li class="f-l text-c" style="width:14%"></li>' +
                                 '                                            <li class="f-l text-c" style="width:10%"></li>' +
                                 '                                            <li class="f-l text-c" style="width:13%"></li>' +
                                 '                                            <li class="f-l text-c" style="width:10%"><span class="ml-5 category-name">'+level3List[k].afterCategoryName+'</span><span class="ml-5 category-id hide">'+level3List[k].afterCategoryId+'</span><span class="ml-5 category-level hide">'+level3List[k].categoryLevel+'</span></li>' +
                                 '                                            <li class="f-l text-c" style="width:13%">' +
                                 '                                                <span class="show-category-name">'+dealNull(level3List[k].fristCategoryName)+'</span>' +
                                 '                                                <i class="Hui-iconfont f-16 c-primary change_third do-edit" style="display: none" onclick="changeFirst(this)">&#xe6df;</i>' +
                                 '                                               <b class="remove-showName" style="display: none"><i class="Hui-iconfont c-primary f-16 va-m"  onclick="removeFirst(this)">&#xe6a6;</i></b>' +
                                 '                                                <input type="text" class="input-text w-80" style="display: none" value="'+dealNull(level3List[k].fristCategoryName)+'">' +
                                 '                                                <span class="edit-btn-box" style="display: none"><i class="Hui-iconfont ml-10 iconsave c-primary f-16 va-m" onclick="saveFirst(this)">&#xe6a7;</i><i class="Hui-iconfont c-primary f-16 va-m iconcancel" onclick="cancelFirst(this)">&#xe6a6;</i></span>' +
                                 '                                            </li>' +
                                 '                                            <li class="f-l text-c category-show" style="width:8%"><input type="checkbox" class="checkbox level3-checkBox" '+checkedStr3+' disabled></li>' +
                                 '                                            <li class="f-l text-c category-sort" style="width:6%"><input type="text" class="input-text w-50 level3Sort text-c" oninput = "value=value.replace(/[^0-9]/g,\'\')" onblur="checksort(this,this.value,100)" value="'+level3List[k].showSequence+'" readonly></li>' +
                                 '                                            <li class="f-l text-c" style="width:14.5%"></li>' +
                                 '                                        </ul>' +
                                 '                                    </div>';
                         }
                     }
                     $(e).parents(".level2").find(".level3-box").html(level3Html);
                 }
             }else {
                 layer.msg(data.errorMessage, {icon: 5, time: 3000});
             }
         }
     });
 }
//编辑一级品类
function first_cate(){
    if(isCanEdit){
        isCanEdit = false;
        $(".edit-first-btn").hide();
        $(".level1-btnBox").show();
        $(".level1-content .change_first").show();
        for(var i=0;i<$(".level1-content .show-category-name").length;i++){
            if($(".level1-content .show-category-name")[i].innerHTML!=""){
                $($(".level1-content .remove-showName")[i]).show();
            }
        }
        $(".level1Sort").removeAttr("readonly");
        $(".level1-checkBox").removeAttr("disabled");
        categoryParams = [];
    }else{
        layer.msg("存在未保存的设置，请保存或取消后操作", {icon: 5, time: 3000});
    }
}
//保存一级品类
function save_cate(e){
    if(!isCanEdit){
        saveCategoryFn($(".level1-box .level1 .level1-content"),1);//调用保存修改函数
    }
}
//取消编辑一级品类
function cancel_cate(e){
    if(!isCanEdit){
        searchSubCate();
        isCanEdit = true;
        $(".edit-first-btn").show();
        $(".level1-btnBox").hide();
        $(".do-edit,.remove-showName").hide();
        $(".level1Sort").attr("readonly","readonly");
        $(".level1-checkBox").attr("disabled","disabled");
        categoryParams = [];
    }
}
//编辑二级品类
function edit_secondcate(e){
     console.log( $(e).parents(".level1"))
    if(isCanEdit){
        isCanEdit = false;
        $(e).parents(".level1").find(".level2-box").show();
        $(e).parents(".level1").find(".level2-content .change_second").show();
        if($(e).parents(".level1").find(".level2-content .show-category-name").html()!=""){
            $(e).parents(".level1").find(".level2-content .remove-showName").show();
        }
        $(e).parents(".level1").find(".level2-checkBox").removeAttr("disabled");
        $(e).parents(".level1").find(".level2Sort").removeAttr("readonly");
        $(e).parents(".level1").find(".index_first").html("-");
        $(e).siblings().show();
        $(e).hide();
        categoryParams = [];
    } else{
        layer.msg("存在未保存的设置，请保存或取消后操作", {icon: 5, time: 3000});
    }
}
//保存二级品类
function save_secondcate(e){
    if(!isCanEdit){
        saveCategoryFn($(e).parents(".level1").find(".level2-box .level2 .level2-content"),2);//调用保存修改函数
    }
}
//取消编辑二级品类
function cancel_secondcate(e){
    if(!isCanEdit){
        uadateCatagoryList(e);
        isCanEdit = true;
        $(e).parents(".level1").find(".level2Sort").attr("readonly","readonly");
        $(e).parents(".level1").find(".level2-checkBox").attr("disabled","disabled");
        $(".do-edit,.remove-showName").hide();
        $(e).parent().children().hide();
        $(e).parent().find("input:first-child").show();
        categoryParams = [];
    }
}

//编辑三级品类
function edit_thirdcate(e){
    if(isCanEdit){
        isCanEdit = false;
        $(e).parents(".level2").find(".level3-box").show();
        $(e).parents(".level2").find(".level3-content .change_third").show();
        if($(e).parents(".level2").find(".level3-content .show-category-name").html()!=""){
            $(e).parents(".level2").find(".level3-content .remove-showName").show();
        }
        $(e).parents(".level2").find(".level3Sort").removeAttr("readonly");
        $(e).parents(".level2").find(".level3-checkBox").removeAttr("disabled");
        $(e).parents(".level2").find(".index_second").html("-");
        $(e).siblings().show();
        $(e).hide();
        categoryParams = [];
    }else{
        layer.msg("存在未保存的设置，请保存或取消后操作", {icon: 5, time: 3000});
    }
}
//保存三级品类
function save_thirdcate(e){
    if(!isCanEdit){
        saveCategoryFn($(e).parents(".level2").find(".level3-content"),3);//调用保存修改函数
    }
}
//取消编辑三级品类
function cancel_thirdcate(e){
    if(!isCanEdit){
        uadateCatagoryList(e);
        isCanEdit = true;
        $(e).parents(".level2").find(".level3Sort").attr("readonly","readonly");
        $(e).parents(".level2").find(".level3-checkBox").attr("disabled","disabled");
        $(".do-edit,.remove-showName").hide();
        $(e).parent().children().hide();
        $(e).parent().find("input:first-child").show();
        categoryParams = [];
    }
}
//点击显示隐藏二级品类
function level2_show(e){
    $(e).parents(".level1").find(".level2-box").toggle();
    if ($(e).html() == "+"){
        $(e).html("-")
    }else {
        $(e).html("+")
    }
}

//点击显示隐藏三级品类
function level3_show(e){
    $(e).parents(".level2").find(".level3-box").toggle();
    if ($(e).html() == "+"){
        $(e).html("-")
    }else {
        $(e).html("+")
    }
}

//编辑虚拟品类名称
function changeFirst(e) {
    $(e).hide();
    $(e).parent().find("input,.edit-btn-box").show();
    $(e).parent().find(".remove-showName,.show-category-name").hide();
}
 function removeFirst(e){
     $(e).parent().hide();
     $(e).parent().parent().find(".show-category-name").html("");
     $(e).parent().parent().find("input").val("");
 }
function saveFirst(e) {
    var parentEl = $(e).parent().parent();
    var val = parentEl.find("input").val();
    parentEl.find("input,.edit-btn-box").hide();
    parentEl.find(".do-edit,.show-category-name").show();
    parentEl.find(".show-category-name").html(val);
    if(parentEl.find(".show-category-name").html()!=""){
        console.log($(parentEl).find(".remove-showName"))
        parentEl.find(".remove-showName").show()
    }else {
        parentEl.find(".remove-showName").hide();
    }
}
function cancelFirst(e) {
    var parentEl = $(e).parent().parent();
    parentEl.find(".do-edit,.show-category-name").show();
    parentEl.find("input,.edit-btn-box").hide();
    var val = parentEl.find("input").val();
    if(parentEl.find(".show-category-name").html()!=""){
        parentEl.find(".remove-showName").show();
    }
}




